home *** CD-ROM | disk | FTP | other *** search
- Path: sun001.spd.dsccc.com!jmccarty
- From: jmccarty@sun1307.spd.dsccc.com (Mike McCarty)
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: Q: '\n' character - strtok trick
- Date: 4 Apr 1996 22:49:49 GMT
- Organization: DSC Communications Corporation
- Message-ID: <4k1jmd$ifr@sun001.spd.dsccc.com>
- References: <31616F63.481D@lava.weeg.uiowa.edu> <828493319snz@genesis.demon.co.uk> <828542474.7672@tertio.demon.co.uk> <4ju9m0$nq9@belle.bork.com>
- NNTP-Posting-Host: sun1307.spd.dsccc.com
-
- In article <4ju9m0$nq9@belle.bork.com>, BOFH <root@belle.bork.com> wrote:
- )alord@tertio.co.uk wrote:
- ): Although the behaviour of strtok() is well defined I try to avoid it like the
- ): plague because it uses a static area to maintain state. If another function
- ): happens to call it between successive invocations then your screwd.
- )
- )Well, yes it does use a static data area to store it's state, but that
- )doesn't matter. When you call the function this way, strtok will return
- )a pointer to the beginning of the string that it chopped a newline off
- )of, AND will insert a NULL character into the location of the newline.
- )After it returns, you are done. Your string has had the newline taken
- )off, and you are done with strtok. strtok will internally remember where
- )it last took a delimiter off, but it only needs it when strtok is called
- )with a NULL buffer anyway.
- )
- )The only problem with this is that you could be in the middle of parsing
- )a string with strtok, and then you attempted to strtok(buffer,...), which
- )would definately cause a problem.
- )
- )My $0.02.
- )
- )
- )Randy Scott <scottr@belle.bork.com>
-
-
- I usually do this (from memory only, so argument order etc. may be off):
-
- boolean readline(char *buffer,size_t max_size,FILE *stream) {
- boolean result;
- size_t place;
-
- if (fgets(buffer,max_size,stream) == NULL)
- result = false;
- else {
- place = strlen(buffer)-1;
- if (place >= 0 && buffer[place] == '\n')
- buffer[place] = '\0';
- result = true;
- }
- return result;
- }
-
- And then exclusively call readline(.,.,.) in my program.
-
- Mike
- --
- ----
- char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
-
- I don't speak for DSC. <- They make me say that.
-